import time
import config.config
import tools.str_render

if 'std_print' not in locals().keys():
    std_print = print


def output_log(strings: str):
    if config.config.LOG_FILE_PATH != "":
        with open(config.config.LOG_FILE_PATH, "a") as logfile:
            logfile.write(strings+"\n")


def startlog():
    output_log(
        f'----------[Log In {time.strftime("%H:%M:%S")} Start]----------')


def info(from_moudle: str, info_str: str):
    global std_print
    typestr = "INFO"
    output_log(tools.str_render.render(config.config.LOG_TEMPLETE,
               TYPE=typestr, FROM=from_moudle, STR=info_str))
    if config.config.LOG_PRINT_COLORFUL:
        typestr = config.config.INFO_COLOR+typestr+config.config.DEFAULT_COLOR
    std_print(config.config.DEFAULT_COLOR+tools.str_render.render(config.config.LOG_TEMPLETE,
                                                                  TYPE=typestr, FROM=from_moudle, STR=info_str))


def wrn(from_moudle: str, info_str: str):
    global std_print
    typestr = "WARNING"
    output_log(tools.str_render.render(config.config.LOG_TEMPLETE,
               TYPE=typestr, FROM=from_moudle, STR=info_str))
    if config.config.LOG_PRINT_COLORFUL:
        typestr = config.config.WRN_COLOR+typestr+config.config.DEFAULT_COLOR
    std_print(config.config.DEFAULT_COLOR+tools.str_render.render(config.config.LOG_TEMPLETE,
                                                                  TYPE=typestr, FROM=from_moudle, STR=info_str))


def err(from_moudle: str, info_str: str):
    global std_print
    typestr = "ERROR"
    output_log(tools.str_render.render(config.config.LOG_TEMPLETE,
               TYPE=typestr, FROM=from_moudle, STR=info_str))
    if config.config.LOG_PRINT_COLORFUL:
        typestr = config.config.ERR_COLOR+typestr+config.config.DEFAULT_COLOR
    std_print(config.config.DEFAULT_COLOR+tools.str_render.render(config.config.LOG_TEMPLETE,
                                                                  TYPE=typestr, FROM=from_moudle, STR=info_str))


def print(*info_strs):
    global std_print
    info_str = ""
    for i in info_strs:
        if len(info_str) != 0:
            info_str = info_str+" "
        info_str = info_str+str(i)
    from_moudle = "stdout"
    typestr = "DEBUG"
    output_log(tools.str_render.render(config.config.LOG_TEMPLETE,
               TYPE=typestr, FROM=from_moudle, STR=str(info_str)))
    if config.config.LOG_PRINT_COLORFUL:
        typestr = config.config.DBG_COLOR+typestr+config.config.DEFAULT_COLOR
    std_print(config.config.DEFAULT_COLOR+tools.str_render.render(config.config.LOG_TEMPLETE,
                                                                  TYPE=typestr, FROM=from_moudle, STR=str(info_str)))
